package com.client.ctrl;

import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import com.client.service.ClientService;
import com.client.view.BasicSetPanel;
import com.client.view.CampusDialog;
import com.client.view.ClassDialog;
import com.client.view.CollageDialog;
import com.client.view.DepartmentDialog;
import com.client.view.GradeDialog;
import com.client.view.MajorDialog;
import com.client.view.RoatInfoManagePanel;
import com.entity.Announcement;
import com.entity.Campus;
import com.entity.Collage;
import com.entity.Department;
import com.entity.Grade;
import com.entity.Major;
import com.entity.Class;
import com.entity.Message;
import com.entity.Result;
import com.entity.User;
import com.util.Command;
import com.util.FileTest;
import com.util.MyTable;
import com.util.MyTable2;

public class RoatCtrl {
  ClientService clientService=new ClientService();
  public RoatCtrl(){}
  
	

	public void basicSet(JPanel panel) {
		int n=panel.getComponentCount();
		for(int i=1;i<n;i++){
			panel.remove(panel.getComponent(i));
		}
		JPanel basicSetPanel=new BasicSetPanel();
		panel.add(basicSetPanel);
	}

	public Vector<Vector<Object>> tabChange(int index,String id) {
		Vector<Vector<Object>> dataValues = null;
		if(index==0){
			dataValues=queryCampuses();
		}else if(index==1){
			dataValues=queryCollages(id);
		}else if(index==2){
			dataValues=queryDepartments(id);
		}else if(index==3){
			dataValues=queryMajors(id);
		}else if(index==4){
			dataValues=queryGrades();
		}else{
			dataValues=queryClasses(id);
		}
		
		
		for(Vector<Object> data:dataValues){
			if(index!=0 && index != 4){
				data.remove(data.size()-1);
				if(index == 5){
					data.remove(data.size()-1);
				}
			}
			    data.remove(0);
				data.add(0, false);
				data.add("<html><i><u>修改</u></i></html>");
			
		}
		return dataValues;
	}

	@SuppressWarnings("unchecked")
	private Vector<Vector<Object>> queryClasses(String id) {
		Vector<Vector<Object>> classes=null;
		Message message=new Message(null,Command.CLASS_QUERY_ALL,id);
		try {
			Result result=clientService.service(message);
			classes=(Vector<Vector<Object>>)result.getObject();
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	return classes;
	}



	@SuppressWarnings("unchecked")
	private Vector<Vector<Object>> queryGrades() {
		Vector<Vector<Object>> grades=null;
		Message message=new Message(null,Command.GRADE_QUERY_ALL,null);
		try {
			Result result=clientService.service(message);
			grades=(Vector<Vector<Object>>)result.getObject();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	return grades;
	}



	@SuppressWarnings("unchecked")
	private Vector<Vector<Object>> queryMajors(String id) {
		Vector<Vector<Object>> majors=null;
		Message message=new Message(null,Command.MAJOR_QUERY_ALL,id);
		try {
			Result result=clientService.service(message);
			majors=(Vector<Vector<Object>>)result.getObject();
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	return majors;
	}



	@SuppressWarnings("unchecked")
	public Vector<Vector<Object>> queryDepartments(String id) {
		 Vector<Vector<Object>> departments=null;
			Message message=new Message(null,Command.DEPARTMENT_QUERY_ALL,id);
			try {
				Result result=clientService.service(message);
				departments=(Vector<Vector<Object>>)result.getObject();
				
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return departments;
	}



	@SuppressWarnings("unchecked")
	public Vector<Vector<Object>> queryCampuses() {
		Vector<Vector<Object>> campuses=null;
			Message message=new Message(null,Command.CAMPUS_QUERY_ALL,null);
			try {
				Result result=clientService.service(message);
				campuses=(Vector<Vector<Object>>)result.getObject();
				
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return campuses;
	}
	
	@SuppressWarnings("unchecked")
	public Vector<Vector<Object>> queryCollages(String id) {
		   Vector<Vector<Object>> collages=null;
			Message message=new Message(null,Command.COLLAGE_QUERY_ALL,id);
			try {
				Result result=clientService.service(message);
				collages=(Vector<Vector<Object>>)result.getObject();
				
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return collages;
	}
	
	public void updateCampus(JTable table,String name) {
			Message message=new Message(null,Command.CAMPUS_QUERY, name);
			Result result;
			try {
				result = clientService.service(message);
				Campus campus=(Campus)result.getObject();
				new CampusDialog(campus).createCampusDialog(table);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
		
		
	}
	public Campus queryCampus(String name) {
		Message message=new Message(null,Command.CAMPUS_QUERY, name);
		
		Campus campus = null;
		try {
			Result result = clientService.service(message);
			campus=(Campus)result.getObject();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	return campus;
	
}
	

	public void saveCampus(JButton okButton,JTable table,JPanel contentPanel,String campusno, String name, String loc) {
		Campus campus=new Campus(campusno, name, loc);
		Message message=null;
		if(okButton.getActionCommand().equals("update")){
			message=new Message(null,Command.CAMPUS_UPDATE,campus);
		}else{
			message=new Message(null, Command.CAMPUS_ADD,campus);
		}
		
			Result result;
			try {
				result = clientService.service(message);
				int row=result.getState();
				if(row==0){
					JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='red'>学校编码重复，请重新输入</font></h></html>"),"错误信息",JOptionPane.ERROR_MESSAGE);
					new CampusDialog(null).createCampusDialog(table);
				}else{
					JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='green'>保存成功</font></h></html>"),"提示信息",JOptionPane.OK_OPTION);
						
						Vector<String> columnsNames=new Vector<String>();
						columnsNames.add("选中");
						columnsNames.add("校区名");
						columnsNames.add("地址");
						columnsNames.add("操作");

						JScrollPane scrollPane=(JScrollPane)(table.getParent().getParent());
						table=new MyTable(tabChange(0,null),columnsNames,new int[]{100,300,600,100},"校区表");
				        scrollPane.setViewportView(table);
                    //scrollPane.add(new MyTable(campuses,columnsNames,new int[]{100,300,600,100}));
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}


	public void delcampusCtrl(JScrollPane scrollPane) {
		MyTable table=(MyTable)scrollPane.getViewport().getComponent(0);
		List<String> delCampusList=new ArrayList<String>();
		for(int i=0;i<table.getRowCount();i++){
			 String isSelected=String.valueOf(table.getValueAt(i, 0));
			 if("true".equals(isSelected)){
				 delCampusList.add(String.valueOf(table.getValueAt(i, 1)));
			 }
		}
		if(delCampusList.size()<=0){
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>请选择要删除的校区</font></h></html>"));
		}else{
		Message message=new Message(null, Command.CAMPUS_DEL, delCampusList);
		try {
			Result result=clientService.service(message);
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>成功删除"+result.getState()+"条记录</font></h></html>"));
			Vector<String> columnsNames=new Vector<String>();
			columnsNames.add("选中");
			columnsNames.add("校区名");
			columnsNames.add("地址");
			columnsNames.add("操作");

			table=new MyTable(tabChange(0,null),columnsNames,new int[]{100,300,600,100},"校区表");
	        scrollPane.setViewportView(table);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	}

	

	public void saveCollage(JButton okButton, MyTable table,
			JPanel contentPanel, String campusNo, String collageNo, String name) {
		Collage collage=new Collage(collageNo, name, campusNo);
		Message message=null;
		if(okButton.getActionCommand().equals("update")){
			message=new Message(null,Command.COLLAGE_UPDATE,collage);
		}else{
			message=new Message(null, Command.COLLAGE_ADD,collage);
		}
		
			Result result;
			try {
				result = clientService.service(message);
				int row=result.getState();
				if(row==0){
					JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='red'>学院编码重复，请重新输入</font></h></html>"),"错误信息",JOptionPane.ERROR_MESSAGE);
					new CollageDialog(null).createCollageDialog(table);
				}else{
					JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='green'>保存成功</font></h></html>"));
						
						Vector<String> columnsNames=new Vector<String>();
						columnsNames.add("选中");
						columnsNames.add("学院");
						columnsNames.add("操作");

						JScrollPane scrollPane=(JScrollPane)(table.getParent().getParent());
						table=new MyTable(tabChange(1,campusNo),columnsNames,new int[]{100,900,100},"学院表");
				        scrollPane.setViewportView(table);
                    //scrollPane.add(new MyTable(collagees,columnsNames,new int[]{100,300,600,100}));
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	public void delCollageCtrl(JScrollPane scrollPane,String campusNo) {
		MyTable table=(MyTable)scrollPane.getViewport().getComponent(0);
		List<String> delCollageList=new ArrayList<String>();
		for(int i=0;i<table.getRowCount();i++){
			 String isSelected=String.valueOf(table.getValueAt(i, 0));
			 if("true".equals(isSelected)){
				 delCollageList.add(String.valueOf(table.getValueAt(i, 1)));
			 }
		}
		if(delCollageList.size()<=0){
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>请选择要删除的学院</font></h></html>"));
		}else{
		Message message=new Message(null, Command.COLLAGE_DEL,delCollageList);
		try {
			Result result=clientService.service(message);
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>成功删除"+result.getState()+"条记录</font></h></html>"));
			Vector<String> columnsNames=new Vector<String>();
			columnsNames.add("选中");
			columnsNames.add("学院");
			columnsNames.add("操作");

			table=new MyTable(tabChange(1,campusNo),columnsNames,new int[]{100,900,100},"学院表");
	        scrollPane.setViewportView(table);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		
	}

	public void updateCollage(MyTable table, String name) {
		Message message=new Message(null,Command.COLLAGE_QUERY, name);
		Result result;
		try {
			result = clientService.service(message);
			Collage collage=(Collage)result.getObject();
			new CollageDialog(collage).createCollageDialog(table);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Collage queryCollage(String name) {
		Message message=new Message(null,Command.COLLAGE_QUERY, name);
		Collage collage = null;
		try {
			Result result = clientService.service(message);
			collage=(Collage)result.getObject();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return collage;
	}




	public void saveDepartment(JButton okButton, MyTable table,
			JPanel contentPanel, String collageNo, String departmentNo,
			String name) {
		
			Department department=new Department(departmentNo, collageNo, name);
			Message message=null;
			if(okButton.getActionCommand().equals("update")){
				message=new Message(null,Command.DEPARTMENT_UPDATE,department);
			}else{
				message=new Message(null, Command.DEPARTMENT_ADD,department);
			}
			
				Result result;
				try {
					result = clientService.service(message);
					int row=result.getState();
					if(row==0){
						JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='red'>学系编码重复，请重新输入</font></h></html>"),"错误信息",JOptionPane.ERROR_MESSAGE);
						new DepartmentDialog(new Department(null, collageNo, null)).createDepartmentDialog(table);
					}else{
						JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='green'>保存成功</font></h></html>"));
							
							Vector<String> columnsNames=new Vector<String>();
							columnsNames.add("选中");
							columnsNames.add("学系");
							columnsNames.add("操作");

							JScrollPane scrollPane=(JScrollPane)(table.getParent().getParent());
							table=new MyTable(tabChange(2,collageNo),columnsNames,new int[]{100,900,100},"学系表");
					        scrollPane.setViewportView(table);
	                    //scrollPane.add(new MyTable(collagees,columnsNames,new int[]{100,300,600,100}));
					}
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
	}



	public void delDepartmentCtrl(JScrollPane scrollPane, String collageNo) {
		MyTable table=(MyTable)scrollPane.getViewport().getComponent(0);
		List<String> delDepartmentList=new ArrayList<String>();
		for(int i=0;i<table.getRowCount();i++){
			 String isSelected=String.valueOf(table.getValueAt(i, 0));
			 if("true".equals(isSelected)){
				 delDepartmentList.add(String.valueOf(table.getValueAt(i, 1)));
			 }
		}
		if(delDepartmentList.size()<=0){
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>请选择要删除的学系</font></h></html>"));
		}else{
		Message message=new Message(null, Command.DEPARTMENT_DEL,delDepartmentList);
		try {
			Result result=clientService.service(message);
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>成功删除"+result.getState()+"条记录</font></h></html>"));
			Vector<String> columnsNames=new Vector<String>();
			columnsNames.add("选中");
			columnsNames.add("学系");
			columnsNames.add("操作");

			table=new MyTable(tabChange(2,collageNo),columnsNames,new int[]{100,900,100},"学系表");
	        scrollPane.setViewportView(table);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		
	}



	public void updateDepartment(MyTable table, String name) {
		Message message=new Message(null,Command.DEPARTMENT_QUERY, name);
		Result result;
		try {
			result = clientService.service(message);
			Department department=(Department)result.getObject();
			new DepartmentDialog(department).createDepartmentDialog(table);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public Department queryDepartment(String name) {
		Message message=new Message(null,Command.DEPARTMENT_QUERY, name);
		Department department = null;
		try {
			Result result = clientService.service(message);
			department=(Department)result.getObject();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return department;
	}


	public void saveMajor(JButton okButton, MyTable table, JPanel contentPanel,
			String deptNo, String majorNo, String name) {
		
		Major major=new Major(majorNo, name, deptNo);
		Message message=null;
		if(okButton.getActionCommand().equals("update")){
			message=new Message(null,Command.MAJOR_UPDATE,major);
		}else{
			message=new Message(null, Command.MAJOR_ADD,major);
		}
		
			Result result;
			try {
				result = clientService.service(message);
				int row=result.getState();
				if(row==0){
					JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='red'>专业编码重复，请重新输入</font></h></html>"),"错误信息",JOptionPane.ERROR_MESSAGE);
					new MajorDialog(new Major(null, null,deptNo)).createMajorDialog(table);
				}else{
					JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='green'>保存成功</font></h></html>"));
						
						Vector<String> columnsNames=new Vector<String>();
						columnsNames.add("选中");
						columnsNames.add("专业");
						columnsNames.add("操作");

						JScrollPane scrollPane=(JScrollPane)(table.getParent().getParent());
						table=new MyTable(tabChange(3,deptNo),columnsNames,new int[]{100,900,100},"专业表");
				        scrollPane.setViewportView(table);
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	
	}



	public void delMajorCtrl(JScrollPane scrollPane, String deptNo) {
		MyTable table=(MyTable)scrollPane.getViewport().getComponent(0);
		List<String> delDepartmentList=new ArrayList<String>();
		for(int i=0;i<table.getRowCount();i++){
			 String isSelected=String.valueOf(table.getValueAt(i, 0));
			 if("true".equals(isSelected)){
				 delDepartmentList.add(String.valueOf(table.getValueAt(i, 1)));
			 }
		}
		if(delDepartmentList.size()<=0){
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>请选择要删除的专业</font></h></html>"));
		}else{
		Message message=new Message(null, Command.MAJOR_DEL,delDepartmentList);
		try {
			Result result=clientService.service(message);
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>成功删除"+result.getState()+"条记录</font></h></html>"));
			Vector<String> columnsNames=new Vector<String>();
			columnsNames.add("选中");
			columnsNames.add("专业");
			columnsNames.add("操作");

			table=new MyTable(tabChange(3,deptNo),columnsNames,new int[]{100,900,100},"专业表");
	        scrollPane.setViewportView(table);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	}



	public void updateMajor(MyTable table, String name) {
		Message message=new Message(null,Command.MAJOR_QUERY, name);
		Result result;
		try {
			result = clientService.service(message);
			Major major=(Major)result.getObject();
			new MajorDialog(major).createMajorDialog(table);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
   
	public Major queryMajor(String name) {
		Message message=new Message(null,Command.MAJOR_QUERY, name);
		Major major=null;
		try {
			Result result = clientService.service(message);
			major=(Major)result.getObject();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return major;
	}


	public void saveGrade(JButton okButton, MyTable table, JPanel contentPanel,
			String gradeNo, String name) {
		Grade grade=new Grade(gradeNo, name);
		Message message=null;
		if(okButton.getActionCommand().equals("update")){
			message=new Message(null,Command.GRADE_UPDATE,grade);
		}else{
			message=new Message(null, Command.GRADE_ADD,grade);
		}
		
			Result result;
			try {
				result = clientService.service(message);
				int row=result.getState();
				if(row==0){
					JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='red'>年级编码重复，请重新输入</font></h></html>"),"错误信息",JOptionPane.ERROR_MESSAGE);
					new GradeDialog(null).createGradeDialog(table);
				}else{
					JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='green'>保存成功</font></h></html>"),"提示信息",JOptionPane.OK_OPTION);
						
						Vector<String> columnsNames=new Vector<String>();
						columnsNames.add("选中");
						columnsNames.add("年级");
						columnsNames.add("操作");

						JScrollPane scrollPane=(JScrollPane)(table.getParent().getParent());
						table=new MyTable(tabChange(4,null),columnsNames,new int[]{100,900,100},"年级表");
				        scrollPane.setViewportView(table);
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}



	public void delGradeCtrl(JScrollPane scrollPane) {
		MyTable table=(MyTable)scrollPane.getViewport().getView();
		List<String> delGradeList=new ArrayList<String>();
		for(int i=0;i<table.getRowCount();i++){
			 String isSelected=String.valueOf(table.getValueAt(i, 0));
			 if("true".equals(isSelected)){
				 delGradeList.add(String.valueOf(table.getValueAt(i, 1)));
			 }
		}
		if(delGradeList.size()<=0){
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>请选择要删除的年级</font></h></html>"));
		}else{
		Message message=new Message(null, Command.GRADE_DEL, delGradeList);
		try {
			Result result=clientService.service(message);
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>成功删除"+result.getState()+"条记录</font></h></html>"));
			Vector<String> columnsNames=new Vector<String>();
			columnsNames.add("选中");
			columnsNames.add("年级");
			columnsNames.add("操作");

			table=new MyTable(tabChange(4,null),columnsNames,new int[]{100,300,600,100},"年级表");
	        scrollPane.setViewportView(table);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	}



	public void updateGrade(MyTable table, String name) {
		Message message=new Message(null,Command.GRADE_QUERY, name);
		Result result;
		try {
			result = clientService.service(message);
			Grade grade=(Grade)result.getObject();
			new GradeDialog(grade).createGradeDialog(table);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Grade queryGrade(String name) {
		Message message=new Message(null,Command.GRADE_QUERY, name);
		Grade grade=null;
		try {
			Result result = clientService.service(message);
			grade=(Grade)result.getObject();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return grade;
	}
	public void saveClass(JButton okButton, MyTable table, JPanel contentPanel,
			String classNo, String majorNo, String gradeNo, String name) {
		Class cla=new Class(classNo, name, majorNo,gradeNo);
		Message message=null;
		if(okButton.getActionCommand().equals("update")){
			message=new Message(null,Command.CLASS_UPDATE,cla);
		}else{
			message=new Message(null, Command.CLASS_ADD,cla);
		}
		
			Result result;
			try {
				result = clientService.service(message);
				int row=result.getState();
				if(row==0){
					JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='red'>班级编码重复，请重新输入</font></h></html>"),"错误信息",JOptionPane.ERROR_MESSAGE);
					new ClassDialog(new Class(null, null,majorNo,gradeNo)).createClassDialog(table);
				}else{
					JOptionPane.showMessageDialog(contentPanel,new JLabel("<html><h><font color='green'>保存成功</font></h></html>"));
						
						Vector<String> columnsNames=new Vector<String>();
						columnsNames.add("选中");
						columnsNames.add("班级");
						columnsNames.add("操作");

						JScrollPane scrollPane=(JScrollPane)(table.getParent().getParent());
						table=new MyTable(tabChange(5,majorNo+","+gradeNo),columnsNames,new int[]{100,900,100},"班级表");
				        scrollPane.setViewportView(table);
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	
	}



	public void delClass(JScrollPane scrollPane,String mgNo) {
		MyTable table=(MyTable)scrollPane.getViewport().getComponent(0);
		List<String> classNameList=new ArrayList<String>();
		for(int i=0;i<table.getRowCount();i++){
			 String isSelected=String.valueOf(table.getValueAt(i, 0));
			 if("true".equals(isSelected)){
				 classNameList.add(String.valueOf(table.getValueAt(i, 1)));
			 }
		}
		if(classNameList.size()<=0){
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>请选择要删除的班级</font></h></html>"));
		}else{
		Message message=new Message(null, Command.CLASS_DEL,classNameList);
		try {
			Result result=clientService.service(message);
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>成功删除"+result.getState()+"条记录</font></h></html>"));
			Vector<String> columnsNames=new Vector<String>();
			columnsNames.add("选中");
			columnsNames.add("班级");
			columnsNames.add("操作");

			table=new MyTable(tabChange(5,mgNo),columnsNames,new int[]{100,900,100},"班级表");
	        scrollPane.setViewportView(table);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	}



	public void updateClass(MyTable table, String name) {
		Message message=new Message(null,Command.CLASS_QUERY, name);
		Result result;
		try {
			result = clientService.service(message);
			Class cla=(Class)result.getObject();
			new ClassDialog(cla).createClassDialog(table);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



	public Vector<Vector<Object>> InfoChange(int index) {
		Vector<Vector<Object>> dataValues = null;
		if(index==0){
			dataValues=queryDataValues("公告");
		}else if(index==1){
			dataValues=queryDataValues("教师");
		}else if(index==10){
			dataValues=queryDataValues("学生");
		}
		
		for(Vector<Object> data:dataValues){
				data.add(0, false);
				data.add(1,"<html><i>"+data.get(1)+"</i></html>");
				data.remove(2);
		}
		
		return dataValues;
	}



	@SuppressWarnings("unchecked")
	private Vector<Vector<Object>> queryDataValues(String objectType) {
		Vector<Vector<Object>> datavalues=null;
		Message message=null;
		if(objectType.equals("公告")){
			message=new Message(null,Command.ANNOUNCEMENT_QUERY_ALL,null);
		}else if(objectType.equals("教师")){
			message=new Message(null,Command.TEACHER_QUERY_ALL,null);
		}else if(objectType.equals("学生")){
			message=new Message(null,Command.STUDENT_QUERY_ALL,null);
		}
		try {
			Result result=clientService.service(message);
			datavalues=(Vector<Vector<Object>>)result.getObject();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	return datavalues;
	}

	public void updateAnnouncement(MyTable2 table, String title, User user) {
		Message message=new Message(null,Command.ANNOUNCEMENT_QUERY, title);
		Result result;
		try {
			result = clientService.service(message);
			Announcement announcement=(Announcement)result.getObject();
			JScrollPane scrollPane=(JScrollPane)table.getParent().getParent();
			JPanel announcementPanel=(JPanel)scrollPane.getParent();
			JTabbedPane tabbedPane=(JTabbedPane)announcementPanel.getParent();
			RoatInfoManagePanel.createAnnounceInfoPanel(announcement, scrollPane);
			//tabbedPane.setComponentAt(0,RoatInfoManagePanel.createAnnounceInfoPanel(announcement, scrollPane));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	@SuppressWarnings("deprecation")
	public boolean saveAnnouncement(JPanel announceInfoPanel, JScrollPane scrollPane, JButton button,
			String announceNo, JTextField titleField, JTextPane contentPane,
			JTextField createPersonField, JTextField createTimeField,
			JComboBox<String> viewBox, ButtonGroup buttonGroup, JTextPane remarkPane,User user) {
		String title=titleField.getText();
	    String content=contentPane.getText();
	    String createTime=createTimeField.getText();
	    boolean save=false;
	    try {
	    	String buttonName=button.getText();
	    	int releaseState=0;
	    	if(buttonName.equals("保存")){
			    releaseState=Integer.parseInt(buttonGroup.getSelection().getActionCommand());
	    	}else if(buttonName.equals("发布")){
			    releaseState=1;
	    	}else if(buttonName.equals("撤销")){
	    		releaseState=2;
	    	}
		    int view=viewBox.getSelectedIndex()+1;
		    String remark=remarkPane.getText();
		    String userId=createPersonField.getText();
		    if(title.equals("")){
				JOptionPane.showMessageDialog(announceInfoPanel, new JLabel("<html><h><font color='red'>标题不能为空</font></h></html>"));
		    }else if(content.equals("")){
				JOptionPane.showMessageDialog(announceInfoPanel, new JLabel("<html><h><font color='red'>标题不能为空</font></h></html>"));
		    }else if(content.length()>600){
				JOptionPane.showMessageDialog(announceInfoPanel, new JLabel("<html><h><font color='red'>内容不能超过600字符</font></h></html>"));
		    }else if(title.length()>60){
				JOptionPane.showMessageDialog(announceInfoPanel, new JLabel("<html><h><font color='red'>标题不能超过60字符</font></h></html>"));
		    }else if(remark.length()>60){
				JOptionPane.showMessageDialog(announceInfoPanel, new JLabel("<html><h><font color='red'>备注60字符</font></h></html>"));
		    }else{
		    Announcement announcement=new Announcement(announceNo, title, content, createTime, releaseState, view, remark, userId);
		    Message message=null;
		    if("add".equals(button.getActionCommand())){
		    	message=new Message(null, Command.ANNOUNCEMENT_ADD, announcement);
		    }else{
		    	message=new Message(null, Command.ANNOUNCEMENT_UPDATE, announcement);
		    }
		    Result result=clientService.service(message);
		    int row=result.getState();
		    if(row==1){
				JOptionPane.showMessageDialog(announceInfoPanel, new JLabel("<html><h><font color='green'>保存成功<b>"+row+"</b>条数据</font></h></html>"));
		        save=true;
		    }else{
				JOptionPane.showMessageDialog(announceInfoPanel, new JLabel("<html><h><font color='red'>成功操作<b>"+row+"</b>条数据,保存失败</font></h></html>"));
		    }
            Vector<Vector<Object>> announcements=InfoChange(0);
			Vector<String> columnsNames=new Vector<String>();
			columnsNames.add("选中");
			columnsNames.add("标题");
			columnsNames.add("创建时间");
			columnsNames.add("发布对象");
			columnsNames.add("发布状态");
			final MyTable2 table=new MyTable2(announcements,columnsNames,new int[]{100,500,300,150,150},"公告表",user);
			scrollPane.setViewportView(table);
		    }
		    
		}  catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return save;
	    
	    
	}



	public void delAnnouncements(JScrollPane scrollPane, User user) {
		MyTable2 table=(MyTable2)scrollPane.getViewport().getComponent(0);
		List<String> titleList=new ArrayList<String>();
		for(int i=0;i<table.getRowCount();i++){
			 String isSelected=String.valueOf(table.getValueAt(i, 0));
			 if("true".equals(isSelected)){
				 String titleValue=table.getValueAt(i, 1).toString();
				 String title=titleValue.split("<html><i>")[1].split("</i></html>")[0];
				 titleList.add(title);
			 }
		}
		if(titleList.size()<=0){
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='red'>请选择要删除的公告</font></h></html>"));
		}else{
		Message message=new Message(null, Command.ANNOUNCEMENT_DEL,titleList);
		try {
			Result result=clientService.service(message);
			JOptionPane.showMessageDialog(table, new JLabel("<html><h><font color='green'>成功删除"+result.getState()+"条记录</font></h></html>"));
			Vector<Vector<Object>> announcements=InfoChange(0);
			Vector<String> columnsNames=new Vector<String>();
			columnsNames.add("选中");
			columnsNames.add("标题");
			columnsNames.add("创建时间");
			columnsNames.add("发布对象");
			columnsNames.add("发布状态");
			table=new MyTable2(announcements,columnsNames,new int[]{100,500,300,150,150},"公告表",user);
			scrollPane.setViewportView(table);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	}



	@SuppressWarnings("null")
	public void findDataValues(JScrollPane scrollPane,String findWord, String objectType,User user) {
		Vector<Vector<Object>> dataValues=queryDataValues(objectType);
		if(dataValues!=null && dataValues.size()>0){
			Vector<Vector<Object>> newDataValues=null;
			for(Vector<Object> dataValue:dataValues){
				for(int index=0;;index++){
					if(objectType.equals("教师")&&index==2){
						break;
					}else if(objectType.equals("学生")&&index==3){
						break;
					}
						String value=dataValue.get(index).toString();
						if(value.contains(findWord)){
							dataValue.add(0, false);
							dataValue.add(1,"<html><i>"+dataValue.get(1)+"</i></html>");
							dataValue.remove(2);
							newDataValues.add(dataValue);
						}
				}
			}
			MyTable2 table=null;
			if(objectType.equals("教师")){
				final Vector<String> columnsNames=new Vector<String>();
				columnsNames.add("选中");
				columnsNames.add("教号");
				columnsNames.add("姓名");
				columnsNames.add("性别");
				columnsNames.add("年龄");
				columnsNames.add("学历");
				columnsNames.add("职衔");
				table=new MyTable2(newDataValues,columnsNames,new int[]{100,150,150,100,100,200,300},"教师表",user);
			}else if(objectType.equals("学生")){
				final Vector<String> columnsNames=new Vector<String>();
				columnsNames.add("选中");
				columnsNames.add("学号");
				columnsNames.add("姓名");
				columnsNames.add("班级");
				columnsNames.add("性别");
				columnsNames.add("年龄");
				columnsNames.add("政治面貌");
			table=new MyTable2(newDataValues,columnsNames,new int[]{100,200,200,100,100,250,250},"学生表",user);
			}
			scrollPane.setViewportView(table);
		}
		
		
	}



	

}
